package com.example.areaclean;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.areaclean.dao.Classroom;
import com.example.areaclean.database.Student;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class stu_check_activity extends AppCompatActivity {
    private String state_id;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private String date;
    RequestQueue queue;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_check);

        CheckBox check1 =findViewById(R.id.checkBox1);
        CheckBox check2 =findViewById(R.id.checkBox2);
        CheckBox check3 =findViewById(R.id.checkBox3);
        CheckBox check4 =findViewById(R.id.checkBox4);
        CheckBox check5 =findViewById(R.id.checkBox5);
        final LinearLayout mLayout=(LinearLayout)findViewById(R.id.m_layout);
        Button signin =findViewById(R.id.sign_in);
//        Button btn_ok =findViewById(R.id.btn_ok);

        queue = Volley.newRequestQueue(getApplicationContext());
        pref = getSharedPreferences("data",MODE_PRIVATE);
        jsonRequest("find_stuo",pref.getString("userNo",""));//添加查询教室

        TextView textView2 =(TextView)findViewById(R.id.qdarea);
        textView2.setText(pref.getString("Stu_area",""));



//        btn_ok.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View view) {
////                StringBuilder task = new StringBuilder();
//                int task = 0;
//                //获取子控件数量
//                int count =mLayout.getChildCount();
//                for (int i=0;i<count;i++){
//                    View child = mLayout.getChildAt(i);
//                    if (child instanceof CheckBox){
//                        CheckBox checkBox = (CheckBox) child;
//                        if (checkBox.isChecked()){
//                            task = task+1;
//                        }
//                    }
//                }
////                Log.d("task", count+" ");
//                if (task==count-1){
//                    Toast.makeText(stu_check_activity.this,"已完成任务，确认签到打卡",Toast.LENGTH_SHORT).show();
//                    state_id="1";
//                }
//                else {
//                    Toast.makeText(stu_check_activity.this,"检查是否有未完成任务",Toast.LENGTH_SHORT).show();
//                    state_id="2";
//                }
//            }
//        });
        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int task = 0;
                //获取子控件数量
                int count =mLayout.getChildCount();
                for (int i=0;i<count;i++){
                    View child = mLayout.getChildAt(i);
                    if (child instanceof CheckBox){
                        CheckBox checkBox = (CheckBox) child;
                        if (checkBox.isChecked()){
                            task = task+1;
                        }
                    }
                }
//                Log.d("task", count+" ");
                if (task==count-1){
                    Toast.makeText(stu_check_activity.this,"已完成任务，签到成功",Toast.LENGTH_SHORT).show();
                    state_id="1";
                }
                else {
                    Toast.makeText(stu_check_activity.this,"检查是否有未完成任务",Toast.LENGTH_SHORT).show();
                    state_id="2";
                }

                Intent intent=new Intent(stu_check_activity.this,student_activity.class);
                startActivity(intent);
                getTime();
                jsonRequest2(pref.getString("Stu_area"," "),pref.getString("s_no",""),
                        date,state_id);

            }
        });


    }
    public void getTime(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date=simpleDateFormat.format(calendar.getTime());
        Log.d("Time", simpleDateFormat.format(calendar.getTime()));
    }


    public void jsonRequest(String aim, final String userNo){
        String url= this.getString(R.string.recUrl);

        HashMap<String, String> params = new HashMap<>();
        params.put("forwhat",aim);
        params.put("userNo",userNo);

        JSONObject jsonObject = new JSONObject(params);

        Log.d("Tag",jsonObject.toString());
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<org.json.JSONObject>() {
                    @Override
                    public void onResponse(org.json.JSONObject jsonObject) {
                        Student stuo = JSON.parseObject(jsonObject.toString(),Student.class);
                        Classroom classroom = JSON.parseObject(jsonObject.toString(),Classroom.class);//解析json
                        Log.d("TAG1",jsonObject.toString());

                        if (userNo.equals(stuo.getS_no())){//java判断字符串相等不能用“==”
                            //存储到pref
                            SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
//                            editor.putString("s_no",stuo.getS_no());
//                            editor.putString("s_name",stuo.getS_name());
                            editor.putString("s_class",classroom.getS_class());
                            editor.apply();

                            TextView textView =(TextView)findViewById(R.id.qdname);
                            textView.setText(stuo.getS_name());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError");
                    }
                });
        queue.add(jsObjRequest);
    }

    public void jsonRequest2(String c_no, String s_no, String time, final String state_id){
        String url= "http://192.168.31.32/tp/public/index.php/api/stu_check";

        HashMap<String, String> params = new HashMap<>();
        params.put("c_no",c_no);
        params.put("s_no",s_no);
        params.put("time",time);
        params.put("state_id",state_id);
        JSONObject jsonObject = new JSONObject(params);
        Log.d("AAAAAAA",jsonObject.toString());
        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String a="0";
                        try {
                            a= jsonObject.getString("successful");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        if (a.equals("1")){
//                            Log.d("qiandao", "successful");
//                            Toast.makeText(stu_check_activity.this,"签到成功",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(stu_check_activity.this,"签到失败",Toast.LENGTH_SHORT).show();
//                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError"+volleyError);
                    }
                });
        queue.add(jsonObjectRequest);

    }
}
